/**
 * Created by 011926 on 2014/10/15.
 */
/**
 * 获得新选中的值，格式为 x,x,x
 * 列入 1,2,3
 * 没有新选中时返回 ""
 */
function getNewSelectValue(){
    var obj = $(".process .newSelect");

    if(obj != null && obj.length > 0)
    {
        var one = 1;
        var two = 1;
        var three = 1;
        var index = 1;//当前被点击的是第几个td

        var allTable = $(".process");
        for(var i=0; i<allTable.length;i++)
        {
            var ss = $(allTable[i]).find(".newSelect");
            if(ss != null && ss.length > 0)
            {
                one = i+1;
                break;
            }
        }

        //确定被点击的是第几个td
        var newTds = $(obj).parent("tr").find("td");
        for(var i=0; i<newTds.length ;i++)
        {
            var cc = $(newTds[i]).attr("class");
            if(cc != null && cc.indexOf("newSelect")>=0)
            {
                index = i+1;
                break;
            }
        }

        //父辈td
        var tds = $(obj).parent("tr").siblings("tr").eq(1).find("td");
        var copyIndex = index;
        for(var i=0; i<tds.length; i++)
        {
            var num = $(tds[i]).attr("colspan");
            if(num == null || num =="" || isNaN(num))
            {
                num = 1;
            }
            num = new Number(num);
            if(copyIndex <= num)//
            {
                two = i+1;
                three = copyIndex;
                break;
            }
            else
            {
                copyIndex = copyIndex - num;
            }
        }
        return one+","+two+","+three;
    }
    return "";
}
/**
 * 初始化 被选中的，就是当前所处状态。
 * @param one
 * @param two
 * @param three
 */
function initSelect(one,two,three)
{
    one = new Number(one);
    two = new Number(two);
    three = new Number(three);
    var index = three;
    if(two > 1)
    {
        //找到被选中的是第几个一个td（index）
        $(".process:eq("+(one-1)+") tr:eq(1) td:lt("+(two-1)+")").each(function(i, n){
            var num = $(this).attr("colspan");
            if(num == null)
            {
                num = 1;
            }
            index = index + new Number(num);
        });
    }
    $(".process:eq("+(one-1)+") tr:eq(2) td:eq("+(index-1)+")").addClass("pSelect");
//    $(".process:eq("+(one-1)+") tr:eq(1) td:eq("+(two-1)+")").css({backgroundColor:'#CBCBCB'});
    $(".process:eq("+(one-1)+") tr:eq(0) td").css({backgroundColor:'#ddd'});//CBCBCB
    //设置鼠标悬停
    $(".process").each(function(){
        var targetTds = $(this).find("tr:eq(2) td");
        targetTds.mouseover(function(){

            if(!isSelect(this))
            {
                $(this).css({
                    backgroundColor:'#a8a8cd'
                });
            }
        });
        targetTds.mouseout(function(){
            if(!isSelect(this))
            {
                $(this).css({
                    backgroundColor:''
                });
            }
        });

        //注册点击事件
        targetTds.click(function(){
            if(!isSuccess(this))
            {
                var thisClass = $(this).attr("class");
                if(thisClass && thisClass.indexOf("newSelect")>=0)//如果它之前呗选中了，则在点击代表取消选择
                {
                    $(this).removeClass("newSelect");
                    $(this).css({
                        backgroundColor:''
                    });
                }
                else
                {
                    var old = $(".process .newSelect");
                    if(old)
                    {
                        old.removeClass("newSelect");
                        old.css({
                            backgroundColor:''
                        });
                    }
                    $(this).addClass("newSelect");
                }
            }
        });
    });
}
function isSelect(obj){
    var thisClass = $(obj).attr("class");
    if(thisClass && (thisClass.indexOf("pSelect")>=0 || thisClass.indexOf("newSelect")>=0) )
    {
        return true;
    }
    return false;
}
function isSuccess(obj){
    var thisClass = $(obj).attr("class");
    if(thisClass && (thisClass.indexOf("pSelect")>=0) )
    {
        return true;
    }
    return false;
}